package br.com.j7.jdesk.ui.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.j7.jdesk.ui.bean.WebContext;

public class DateStringConverter extends WebContext implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if( value != null && !value.equals("")){  
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            try {
				return ( Date ) formatter.parse( value );
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}  
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
		if( value != null ){
			return value.toString();
		}
		return null;
	}

}
